package fr.labri.buganalyzer;

import java.io.File;
import java.util.HashSet;

import fr.labri.buganalyzer.model.Change;
import fr.labri.buganalyzer.model.Issue;

public class DatabaseFeeder {

	private String trackerURL;
	private String trackerType;
	private String dbFile;

	public DatabaseFeeder(String trackerURL, String type, String dbFile) {
		this.trackerURL = trackerURL;
		this.trackerType = type;
		this.dbFile = dbFile;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		String inputfolder = args[0];
		String url = args[1];
		String dbSuffix = args[2];
		File directory = new File(inputfolder);

		DatabaseFeeder feeder = new DatabaseFeeder(url, "Bugzilla", "ProductionIssuesDB"+dbSuffix);
		feeder.feed(directory);
	}

	private void feed(File directory) {
		if (directory.exists() && directory.isDirectory()) {
			File[] files = directory.listFiles();
			
			BugAnalyzer analyzer = new BugAnalyzer(this.trackerURL, this.trackerType, this.dbFile);
			for (File fi : files) {
				if (fi.getName().endsWith(".xml")) {
					System.out.println("======= processing "+ fi.getName());
					analyzer.storeIssuesFromFile(fi.getAbsolutePath(), true);
					System.out.println("file " + fi.getName() + " processed: "
					+ analyzer.getExtractor().getIssues().size() +" issues, " +
					analyzer.getExtractor().getChanges().size() +" changes found"); 
					
					//TODO : change the very dirty empty of issues and changes
					System.out.println("======= Tearing down the extractor ========");
					analyzer.getExtractor().setIssues(new HashSet<Issue>());
					analyzer.getExtractor().setChanges(new HashSet<Change>());
					fi.delete(); //In the only purpose to manage disk usage 
				}
			}
		}
	}

	public String getTrackerURL() {
		return trackerURL;
	}

	public void setTrackerURL(String trackerURL) {
		this.trackerURL = trackerURL;
	}

	public String getTrackerType() {
		return trackerType;
	}

	public void setTrackerType(String trackerType) {
		this.trackerType = trackerType;
	}

	public String getDbFile() {
		return dbFile;
	}

	public void setDbFile(String dbFile) {
		this.dbFile = dbFile;
	}
}
